<?PHP
/**
 * OpenStation
 *
 * @Application : View.class.php
 * @Encode : 65001 Unicode ( UTF-8 NO BOM )
 * @Description : 视图控制类
 * @Author : HK <hkyyang@gmail.com>
 */

class View {

    // 模板引擎实例
    private $tpl = NULL;

    private $internalVariable = array();

    /**
     * 构造方法
     *
     * @access protected
     * @param $
     * @return Object
     */
    public function __construct() {
        if( ! class_exists( 'OpenTPL' ) ) {
            import( 'Lib.Template.OpenTPL.OpenTPL_Parse' );
            import( 'Lib.Template.OpenTPL.OpenTPL_Method' );
            import( 'Lib.Template.OpenTPL' );
        }
        $this -> tpl = new OpenTPL;
    }

    /**
     * 模板变量赋值
     *
     * @access protected
     * @param Array | String : $name
     * @param * : $value
     * @return Object
     */
    /**
     * assign 方法，模板变量赋值
     *
     * @access public
     * @param String : $name, 模板变量名称
     * @param * : $value, 模板变量值
     * @return void
     */
    public function assign( $name, $value = NULL ) {
        if( is_array( $name ) ) {
            $this -> internalVariable = array_merge( $this -> internalVariable, $name );
        } else {
            $this -> internalVariable[ $name ] = $value;
        }
    }

    /**
     * fetch 方法，获取模板输出内容
     *
     * @access protected
     * @param String : $tpl
     * @param String : $charset
     * @param String : $contentType
     * @param String : $display 立即显示
     * @return void
     */
    public function fetch( $tpl = NULL, $charset = NULL, $contentType = NULL, $display = TRUE ) {

        $charset = empty( $charset ) ? auto_get_config( 'TEMPLATE.CHARSET' ) : $charset;
        $contentType = empty( $contentType ) ? auto_get_config( 'TEMPLATE.CONTENT_TYPE' ) : $contentType;
        ob_start();
        ob_implicit_flush( 0 );
        header( 'Content-Type:' . $contentType . '; charset=' . $charset );
        header( 'Cache-control: private' );
        header( 'X-Powered-By:OpenStation' . OS_VER );
        extract( $this -> internalVariable, EXTR_OVERWRITE );
        include $this -> tpl -> fetch( $tpl );
        $content = ob_get_clean();

        // 替换模板常量
        $content = str_replace(
            array(
                '__TIMESTAMP__',
                '__LDELIM__',
                '__RDELIM__',
                '__SELF__',
                '__URL__',
                '__ROOT__',
                '__ACTION__',
                '__STATIC__',
                '__STYLE__',
                '__SCRIPT__',
                '__IMAGE__',
            ),
            array(
                time(),
                $this -> tpl -> left,
                $this -> tpl -> right,
                Cache :: get( 'HTTP.SELF' ),
                Cache :: get( 'HTTP.URL' ),
                Cache :: get( 'HTTP.ROOT' ),
                Cache :: get( 'SERVER.METHOD' ),
                string_after_remove( Cache :: get( 'HTTP.STATIC' ), '/' ),
                string_after_remove( Cache :: get( 'HTTP.STATIC' ), '/' ) . '/Styles',
                string_after_remove( Cache :: get( 'HTTP.STATIC' ), '/' ) . '/Scripts',
                string_after_remove( Cache :: get( 'HTTP.STATIC' ), '/' ) . '/Images',
            ),
            $content
        );

        if( $display ) {
            echo $content;
        } else {
            $this -> internalVariable = array();
            return $content;
        }
    }

}
?>